package com.swinw.asumall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.swinw.asumall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016110200785528";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCwmhJPkcwSLDq3LKFkRxGQ51pKTV1kO3CCT0M7yJ+yXhO9VXzgTuBfzHe/H90UBhPyCbNmWfXF7Q7vBXp/Hx3Q9qT0Xn/lGWc7tJr/r6Ec+E+SYt4UQ1j3WsuuKLnuPXZyh1E+N3/GVVfSFXolAFMg4P9SNpudPnGPqnmlN1gyUKt22t7x40H7G/QRAZKW+CpRLUTMatuxnwla4kPnwXceOdAo2N+KuLNQ4bFVLPMST+wqCkrX2PU4hjV3+z1J/+GJGIM/KzHsDhU1ijgPkEdBVW5rWd14Xvx59LY1TQc24Dwyu3KMNh9qlDQzQSAWOeUOTyg0Xr2cJdB5NQmiPmJZAgMBAAECggEAKz3eg+swK7b2vYlJD1OzgmaTniPpXTSMrOuZi9bo+3S7op1/ETFfETXwkDTOHiwK0DX93m7ilg8gm/gccWn+E/+AjnFhIYih1WW4mii3628FlNNui0meTBrISqGgPSyBhzOI+hH+0GJvOXqWyN6WfDN7cPb8DXmPttzIImM1zkO4KAJs/0g30F3DjtbDyFJWhFVULN3VuDZZqZsiCYUZ/QUGg4ltvvutq1Ko2Ys19TvhamsECc06PhJqD6knSObd2tdKHySN8RuEA23PMqrGsfEzexLonxQQrU9PvFu8K1C3lCyakCUHd//Td70NPiuN+RfajPwg0YgD6413MpnkjQKBgQDfR/m2e7m/QnknYGxXYQz6nzmSpwtGhOZBX35ZegpWQ/OLrFbqGxeKTq+wYuNJjXqH6q+z4YvGL3LSe0C820KiPafNuzAN6PV1KdJ42RlAXWlk2kofvTWiMRzjjdq7D/aBjqYSvIOTQ7qRvea0f6OUbtnZxfGQMiDnfKATYg21lwKBgQDKev+k9uqfMDILQ6psUCC2L3EYax7NulIb2SyQZQTHtF6elKlf2lVTE7TlfOM8K0JfgS+CSi3JiWyYMJKiux7WIuKVSIdiI5XfD8G5PRoUAh96v/EJRPhAuL0RmDARTNHI/AvSkPXT+OqV2K9jENLGaRe7H2ZWLgm8mJDFLYYFjwKBgAOpl5QBtYtImNonf79zLRZTVEtuiAXRGkHKQjVPhiniFXWeDxLp/Nnc7o7TIG1Nb8wY8mbd9T/7+UMwmGBArHgi0MTUJDamMpiIZhZrYTmoDpmD5yFcJiwR6CpnYkYdPerkHx9Bjlr1CnNnjQg668D8jIBA+fRmjhmd9rY6roTjAoGBAIA5D/dJnh6dCLP1b9ETlQOKW5ZZD7K8UQ7torj2H+bPpDPMdM4Ml4f6rM2/Hq+VvXx5dLqPextreWePKuz+jnKQuOhMe998JYaSld1WglM1XPYA4wU5tfRWiPTBaX8rJJagdmQsBGDt+rBV/gtANhoLp0mKguYEVYonG1aik4KlAoGAN7kS54SASUMvIIBpJ+9GwLd9lJ6DZLU6l5QabUblw9b3ObxnvRpb0UUXAgz2sRFmyJJUGyiVn7cSiuRrA6Ew4E6LkqvcbbJQRfBUbuDiP6Y5kcU1hYM5qxxDGm3UWG45kJl7zv71c4ZrPaK26X9fGySPoCW92skHL+QL3MdC3M0=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmATQHC5U7n9QNCwxMCFLmQtbU4bZjxy80xlCdemOY8koxz6Zwth3UMylEMxIqgNd5kLGVTHa+dWzrYKn+zx4wxwcRi/nqmPIakkHmlvfsP6qdPBocHn7LzKciPrNRFHt7e59SUsftYlvUu6RdQTIxnbQ54h1ZMvuafqsuBSAr8a3E0pSbLk0GdfstoyuK5v64ugXS0gjT/TWzjDI6MtPI8wxTkKxVL6Rzoxvi5uZx6mmq/lYDyRNvsx3gkB/NvV4jjVXRxe5yA/zVah+IEeUoGU9lhcTWusYrKkbyn19IOQc+krYludV1Bj6HARSBhJhDVMI6ABJUWo/hR2FOGAUcQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://order.swinw.com/payNotify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://order.swinw.com/paySuccess";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
